package com.aos.erp.gov.ex.service.impl;


import com.aos.erp.common.bean.IPageBean;
import com.aos.erp.common.bean.PageBean;
import com.aos.erp.common.dao.mapper.Condition;
import com.aos.erp.common.exception.BizException;
import com.aos.erp.gov.ex.mapper.GovExConfigMapper;
import com.aos.erp.gov.ex.model.GovExConfig;
import com.aos.erp.gov.ex.model.GovExConfigItem;
import com.aos.erp.gov.ex.service.GovExConfigItemService;
import com.aos.erp.gov.ex.service.GovExConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.List;


@Service
public class GovExConfigServiceImpl extends ServiceImpl<GovExConfigMapper, GovExConfig> implements GovExConfigService {

    @Resource
    private GovExConfigItemService itemService;

    @Override
    public GovExConfig getById(long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public GovExConfig saveConfig(GovExConfig bean, List<GovExConfigItem> configItemList) {
        GovExConfig exConfig = getByCode(bean.getProvinceCode(), bean.getCityCode());
        if (exConfig == null) {
            //新增
            bean.setCreateTime(Calendar.getInstance().getTime());
            bean.setUpdateTime(bean.getCreateTime());
            bean.setId(0);//重置
            baseMapper.insert(bean);
            //新增
            for (GovExConfigItem configItem : configItemList) {
                configItem.setParentId(bean.getId());
                configItem.setParentType(bean.getConfigType());
                configItem.setUpdateTime(bean.getUpdateTime());
                itemService.addBean(configItem);
            }
            return bean;
        } else {
            //修改
            if (exConfig.getConfigType() != bean.getConfigType()) {
                exConfig.setUpdateTime(Calendar.getInstance().getTime());
                exConfig.setConfigType(bean.getConfigType());
                baseMapper.updateById(exConfig);
            }
            //修改
            for (GovExConfigItem configItem : configItemList) {
                configItem.setParentId(exConfig.getId());
                configItem.setParentType(exConfig.getConfigType());
                itemService.saveConfigItem(configItem);
            }
            return exConfig;

        }

    }


    @Override
    public GovExConfig getByCode(String provinceCode, String cityCode) {
        Condition condition = new Condition(GovExConfig.class);

        condition.eqFiled("provinceCode", provinceCode);
        condition.eqFiled("cityCode", cityCode);

        return baseMapper.selectOneBean(condition.getWrapper());
    }

    @Override
    public PageBean<GovExConfig> queryByPage(String provinceCode, String cityCode, long pageIndex, long pageSize) {

        IPageBean<GovExConfig> pageBean = baseMapper.createBizPage(pageIndex, pageSize);
        Condition condition = new Condition(GovExConfig.class);
        condition.orderByDesc("id");

        if (StringUtils.isNotBlank(provinceCode)) {
            condition.eqFiled("provinceCode", provinceCode);
        }

        if (StringUtils.isNotBlank(cityCode)) {
            condition.eqFiled("cityCode", cityCode);
        }


        return (PageBean<GovExConfig>) baseMapper.selectPage(pageBean, condition.getWrapper());
    }
}